<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\energy;

use think\facade\Db;
use think\exception\ValidateException;
use app\common\dao\energy\EnergyUsersDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\property\users\PropertyUsersLogRepository;

/**
 * Class EnergyUsersRepository
 *
 * @package app\common\repositories\energy
 * @mixin EnergyUsersDao
 */
class EnergyUsersRepository extends BaseRepository
{

    public function __construct(EnergyUsersDao $dao)
    {
        $this->dao = $dao;
    }


    /**
     * @param array $where
     * @param $page
     * @param $limit
     * @return array
     */
    public function getPropertyList(array $where, $page, $limit, $propertyId)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->with([
                'userInfo' => function ($query) {
                    $query->bind(['username' => 'username', 'nickname' => 'nickname']);
                },
                'energyMeterInfo' => function ($query) {
                    $query->field('id,energy_name,energy_no');
                    $query->bind(['energy_name' => 'energy_name', 'energy_no' => 'energy_no']);
                }
            ])
            ->order('id desc')
            ->select();
        return compact('list', 'count');
    }

    public function geApiUsersList(array $where, $page, $limit, int $propertyId = null)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->with([
                'userInfo' => function ($query) {
                    $query->field('id,username,nickname,mobile');
                    $query->bind(['username' => 'username', 'mobile' => 'mobile', 'nickname' => 'nickname']);
                }
            ])
            ->field('id,property_id,energy_id,user_id,is_main,status,remark')
            ->order('id', 'desc')
            ->select();
        return compact('count', 'list');
    }


    public function addInfo($propertyId, $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $propertyId, $userId) {
            $info = $this->dao->getSearch([])
                ->where('user_id', $data['user_id'])
                ->where('energy_id', $data['energy_id'])
                ->find();
            if ($info) {
                $this->dao->update($info['id'], $data);
            } else {
                $data['property_id'] = $propertyId;
                $info = $this->dao->create($data);
                /** @var PropertyUsersLogRepository $repository */
                $repository = app()->make(PropertyUsersLogRepository::class);
                $repository->addInfo($propertyId, $userId, 'ENERGY.USERS.ADD', $data);
            }
            return $info;
        });
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $info, $userId) {

            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'ENERGY.USERS.EDIT', $data);
            return $this->dao->update($info['id'], $data);
        });
    }

    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }

    public function deleteInfo($info, int $userId = 0)
    {
        /** @var PropertyUsersLogRepository $repository */
        $repository = app()->make(PropertyUsersLogRepository::class);
        $repository->addInfo($info['property_id'], $userId, 'ENERGY.USERS.DEL', $info);
        $this->dao->delete($info['id']);
    }
}
